package com.ls.code01;

/**
 * @author LS
 * @version 1.0
 * @description: 冒泡排序
 * @date 2021/5/3 下午12:40
 */
public class Code03 {

    public static void print(int[] arr) {
        //0~n-1
        //0~n-2
        for (int end = arr.length - 1; end >= 0; end--) {
            for (int i = 0; i < end; i++) {
                if (arr[i] > arr[i + 1]) {
                    swap(arr, i, i + 1);
                }
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 1, 4, 3, 2, 8, 5, 2};
        print(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
